package edu.upc.fib.lamsandroid.client.request.user;

import com.google.gson.Gson;
import com.octo.android.robospice.request.SpiceRequest;
import edu.upc.fib.lamsandroid.client.LamsAndroidClientApplication;
import edu.upc.fib.lamsandroid.server.api.response.user.LogoutResponse;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;

public class LogoutRequest extends SpiceRequest<LogoutResponse> {

    private Gson gson;

    public LogoutRequest() {
        super(LogoutResponse.class);

        gson = new Gson();
    }

    @Override
    public LogoutResponse loadDataFromNetwork() throws Exception {

        LamsAndroidClientApplication app = LamsAndroidClientApplication.getInstance();
        String host = app.getPreferences().getString("host_preference", "");
        int port = Integer.parseInt(app.getPreferences().getString("port_preference", "0"));
        String context = app.getPreferences().getString("context_preference", "");

        HttpPost httppost = new HttpPost("http://" + host + ":" + port + "/" + context + "/user/logout");

        HttpResponse httpResponse = app.doPost(httppost);

        String json = IOUtils.toString(httpResponse.getEntity().getContent());

        return gson.fromJson(json, LogoutResponse.class);
    }
}
